<?php
/**
 * @package Framework
 * @subpackage Scripts
 */

require('../../system.php');

/*
 *	1=cal_events
 * 2=ab_contacts
 * 3=ab_companies
 * 4=no_notes
 * 5=pmProjects
 * 6=folders
 * 7=bs_orders
*/

if($GO_SECURITY->has_admin_permission($GO_SECURITY->user_id))
{
	$db1 = new db();
	$db2 = new db();

	$db1->query("SELECT * FROM links");
	
	while($db1->next_record())
	{
		switch($db1->f('type1'))
		{
			case '1':
				$sql = "SELECT id FROM cal_events WHERE link_id='".$db1->f('link_id1')."';";
				$db2->query($sql);
				if(!$db2->next_record())
				{
					$db2->query("DELETE FROM links WHERE type1='".$db1->f('type1')."' AND link_id1='".$db1->f('link_id1')."';");
					echo 'Deleting link_id '.$db1->f('link_id1').' with type '.$db1->f('type1').'<br />';
				}			
			break;
			
			case '2':
				$sql = "SELECT id FROM ab_contacts WHERE link_id='".$db1->f('link_id1')."';";
				$db2->query($sql);
				if(!$db2->next_record())
				{
					$db2->query("DELETE FROM links WHERE type1='".$db1->f('type1')."' AND link_id1='".$db1->f('link_id1')."';");
					echo 'Deleting link_id '.$db1->f('link_id1').' with type '.$db1->f('type1').'<br />';
				}			
			break;
			
			case '3':
				$sql = "SELECT id FROM pmProjects WHERE link_id='".$db1->f('link_id1')."';";
				$db2->query($sql);
				if(!$db2->next_record())
				{
					$db2->query("DELETE FROM links WHERE type1='".$db1->f('type1')."' AND link_id1='".$db1->f('link_id1')."';");
					echo 'Deleting link_id '.$db1->f('link_id1').' with type '.$db1->f('type1').'<br />';
				}			
			break;
			
			case '4':
				$sql = "SELECT id FROM no_notes WHERE link_id='".$db1->f('link_id1')."';";
				$db2->query($sql);
				if(!$db2->next_record())
				{
					$db2->query("DELETE FROM links WHERE type1='".$db1->f('type1')."' AND link_id1='".$db1->f('link_id1')."';");
					echo 'Deleting link_id '.$db1->f('link_id1').' with type '.$db1->f('type1').'<br />';
				}			
			break;
			
			case '5':
				$sql = "SELECT id FROM no_notes WHERE link_id='".$db1->f('link_id1')."';";
				$db2->query($sql);
				if(!$db2->next_record())
				{
					$db2->query("DELETE FROM links WHERE type1='".$db1->f('type1')."' AND link_id1='".$db1->f('link_id1')."';");
					echo 'Deleting link_id '.$db1->f('link_id1').' with type '.$db1->f('type1').'<br />';
				}			
			break;
			
			case '6':
				$sql = "SELECT path FROM fs_links WHERE link_id='".$db1->f('link_id1')."';";
				$db2->query($sql);
				if(!$db2->next_record())
				{
					$db2->query("DELETE FROM links WHERE type1='".$db1->f('type1')."' AND link_id1='".$db1->f('link_id1')."';");
					echo 'Deleting link_id '.$db1->f('link_id1').' with type '.$db1->f('type1').'<br />';
				}			
			break;
			
			case '7':
				$sql = "SELECT id FROM bs_orders WHERE link_id='".$db1->f('link_id1')."';";
				$db2->query($sql);
				if(!$db2->next_record())
				{
					$db2->query("DELETE FROM links WHERE type1='".$db1->f('type1')."' AND link_id1='".$db1->f('link_id1')."';");
					echo 'Deleting link_id '.$db1->f('link_id1').' with type '.$db1->f('type1').'<br />';
				}			
			break;
		}
		
		
		
		
		switch($db1->f('type2'))
		{
			case '1':
				$sql = "SELECT id FROM cal_events WHERE link_id='".$db1->f('link_id2')."';";
				$db2->query($sql);
				if(!$db2->next_record())
				{
					$db2->query("DELETE FROM links WHERE type2='".$db1->f('type2')."' AND link_id2='".$db1->f('link_id2')."';");
					echo 'Deleting link_id '.$db1->f('link_id2').' with type '.$db1->f('type2').'<br />';
				}			
			break;
			
			case '2':
				$sql = "SELECT id FROM ab_contacts WHERE link_id='".$db1->f('link_id2')."';";
				$db2->query($sql);
				if(!$db2->next_record())
				{
					$db2->query("DELETE FROM links WHERE type2='".$db1->f('type2')."' AND link_id2='".$db1->f('link_id2')."';");
					echo 'Deleting link_id '.$db1->f('link_id2').' with type '.$db1->f('type2').'<br />';
				}			
			break;
			
			case '3':
				$sql = "SELECT id FROM pmProjects WHERE link_id='".$db1->f('link_id2')."';";
				$db2->query($sql);
				if(!$db2->next_record())
				{
					$db2->query("DELETE FROM links WHERE type2='".$db1->f('type2')."' AND link_id2='".$db1->f('link_id2')."';");
					echo 'Deleting link_id '.$db1->f('link_id2').' with type '.$db1->f('type2').'<br />';
				}			
			break;
			
			case '4':
				$sql = "SELECT id FROM no_notes WHERE link_id='".$db1->f('link_id2')."';";
				$db2->query($sql);
				if(!$db2->next_record())
				{
					$db2->query("DELETE FROM links WHERE type2='".$db1->f('type2')."' AND link_id2='".$db1->f('link_id2')."';");
					echo 'Deleting link_id '.$db1->f('link_id2').' with type '.$db1->f('type2').'<br />';
				}			
			break;
			
			case '5':
				$sql = "SELECT id FROM no_notes WHERE link_id='".$db1->f('link_id2')."';";
				$db2->query($sql);
				if(!$db2->next_record())
				{
					$db2->query("DELETE FROM links WHERE type2='".$db1->f('type2')."' AND link_id2='".$db1->f('link_id2')."';");
					echo 'Deleting link_id '.$db1->f('link_id2').' with type '.$db1->f('type2').'<br />';
				}			
			break;
			
			case '6':
				$sql = "SELECT path FROM fs_links WHERE link_id='".$db1->f('link_id2')."';";
				$db2->query($sql);
				if(!$db2->next_record())
				{
					$db2->query("DELETE FROM links WHERE type2='".$db1->f('type2')."' AND link_id2='".$db1->f('link_id2')."';");
					echo 'Deleting link_id '.$db1->f('link_id2').' with type '.$db1->f('type2').'<br />';
				}			
			break;
			
			case '7':
				$sql = "SELECT id FROM bs_orders WHERE link_id='".$db1->f('link_id2')."';";
				$db2->query($sql);
				if(!$db2->next_record())
				{
					$db2->query("DELETE FROM links WHERE type2='".$db1->f('type2')."' AND link_id2='".$db1->f('link_id2')."';");
					echo 'Deleting link_id '.$db1->f('link_id2').' with type '.$db1->f('type2').'<br />';
				}			
			break;
		}
	}
	
	echo 'All done!';
}else
{
	echo 'Please log in as administrator to use this script';
}
